package com.wjf.accesscontrol.util

import android.annotation.SuppressLint
import android.app.AlarmManager
import android.app.PendingIntent
import android.content.Context
import android.os.Process
import androidx.fragment.app.FragmentManager
import com.wjf.accesscontrol.MyApplication
import com.wjf.accesscontrol.ui.read.dialog.ResultDialog
import com.wjf.accesscontrol.ui.read.dialog.ResultDialogCallback
import java.util.Calendar

/**
 * @Description
 * @Author WuJianFeng
 * @Date 2023/9/19 17:20
 *
 */

object AppUtils {

    fun getToken(result:(String,String) -> Unit){
        val timestamp = System.currentTimeMillis().toString()
        result(Md5Util.md5Java("$CONTROL_APPID$CONTROL_SECRET${timestamp}"),timestamp)
    }

    fun showResultDialog(fragmentManager: FragmentManager, resultDialogCallback: ResultDialogCallback? = null,type: String, msg: String = ""){
        val resultDialog = ResultDialog.newInstance(type,resultDialogCallback,msg)
        resultDialog.show(fragmentManager,"ResultDialog")
    }

    /**
     * 主动退出并重启App
     */
    @SuppressLint("UnspecifiedImmutableFlag")
    fun restartApp() {
        val context = MyApplication.instance
        // 获取启动的intent
        val intent = context.packageManager.getLaunchIntentForPackage(context.packageName)
        val restartIntent: PendingIntent =
            PendingIntent.getActivity(context, 0, intent, 0)
        val mgr = context.getSystemService(Context.ALARM_SERVICE) as AlarmManager
        //2秒后重启应用
//        mgr.set(AlarmManager.RTC, System.currentTimeMillis() + 2000, restartIntent)
        mgr.set(AlarmManager.RTC, System.currentTimeMillis(), restartIntent)
        // 重启应用
        Process.killProcess(0)
    }

    /**
     * 字符串的日期格式的计算
     */
    fun daysBetween(smdate: String?, bdate: String?): Int {
        var between_days: Long = 0
        val cal: Calendar = Calendar.getInstance()
        cal.time = TimeUtils.instance.parseTime(smdate,"yyyy-MM-dd")
        val time1 = cal.timeInMillis
        cal.time = TimeUtils.instance.parseTime(bdate,"yyyy-MM-dd")
        val time2 = cal.timeInMillis
        between_days = (time2 - time1) / (1000 * 3600 * 24)
        return between_days.toString().toInt()
    }

    /**
     * 检查是否重复上传
     * 避免卡放在设备上导致一直上传数据问题
     * 当前尝试上传时间与上次尝试上传时间间隔小于5秒，则不上传
     */
    fun repeatCommit(idNumber: String, time: Long, type: Int): Boolean{

        var result = false

        when(type){

            TYPE_1 -> {
                if (idNumber == Constant.idCardStoreEntity.idNumber && time - Constant.idCardStoreEntity.time < 5000) {
                    Constant.idCardStoreEntity.time = time
                    result = true

                }else{
                    Constant.idCardStoreEntity.idNumber = idNumber
                    Constant.idCardStoreEntity.time = time
                    result = false
                }
            }

            TYPE_2 -> {
                if (idNumber == Constant.socialStoreEntity.idNumber && time - Constant.socialStoreEntity.time < 5000) {
                    Constant.socialStoreEntity.time = time
                    result = true

                }else{
                    Constant.socialStoreEntity.idNumber = idNumber
                    Constant.socialStoreEntity.time = time
                    result = false
                }
            }
        }
        return result
    }
}